layui.use(["layer", "laytpl", "element", "form", "slider", "laydate", "rate", "colorpicker", "layedit", "carousel", "upload"],
    function() {
        var $ = layui.jquery,
            layer = layui.layer,
            laytpl = layui.laytpl,
            setter = layui.cache,
            element = layui.element,
            slider = layui.slider,
            laydate = layui.laydate,
            rate = layui.rate,
            colorpicker = layui.colorpicker,
            carousel = layui.carousel,
            form = layui.form,
            upload = layui.upload,
            layedit = layui.layedit;
        $(document).ready(function () {
            layui.use('form', function () {

                form.render();   //表单渲染，得渲染才会有效果显示出来
            })
        })


        // 省下拉框
        $.ajax({
            type:"post",
            url:"provinces",
            contentType: "application/json;charset=utf-8",
            dataType:"json",
            success:function (plist) {
                var phtml ="<option value=''>请选择1</option>";
                for (let i = 0; i < plist.length; i++) {
                    phtml += "<option value="+plist[i].provinceid+">"+plist[i].province+"</option>";
                }

                layer.msg(phtml);
                $("select[name='provinceid']").html(phtml);
                $("select[name='provinceid']").val($("#provincesIdh").val());
                $("select[name='idType']").val($("#idTypeh").val());

                // console.log(111);

                layui.form.render('select'); //这个很重要,没有这个新的option不会显示
                // layer.msg(data.)

            },error:function () {
                layer.msg("添加请求失败1");
            }
        });

        //市下拉框
        $.ajax({
            type: "post",
            url:  "cities",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (clist) {
                layer.msg("1");
                var chtml = "<option value=''>请选择1</option>";
                for (let i = 0; i < clist.length; i++) {
                    if ($("select[name='provinceid']").val() == clist[i].provinceid) {
                        chtml += "<option value=" + clist[i].cityid + ">" + clist[i].city + "</option>";
                    }
                }
                $("select[name='cityid']").html(chtml);
                $("select[name='cityid']").val($("#citiesIdh").val());
                form.render('select'); //这个很重要,没有这个新的option不会显示
                layer.msg(chtml);

            }, error: function () {
                layer.msg("添加请求失败2");
            }
        });

        //区下拉框
        $.ajax({
            type: "post",
            url:  "areas",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (alist)   {
                var ahtml = "<option value=''>请选择1</option>";
                for (let i = 0; i < alist.length; i++) {
                    if ($("select[name='cityid']").val() == alist[i].cityid) {
                        ahtml += "<option value=" + alist[i].areaid + ">" + alist[i].area + "</option>";
                    }
                }
                $("select[name='areaid']").html(ahtml);
                $("select[name='areaid']").val($("#areaidh").val());
                layer.msg(ahtml);
                form.render('select'); //这个很重要,没有这个新的option不会显示
            }, error: function () {
                layer.msg("添加请求失败3");
            }
        });

        //当select发生改变时
        form.on('select(provinceid)', function (dat) {
            $("#cityid").empty();
            $("#areaid").empty();

            //市下拉框
            $.ajax({
                type: "post",
                url:"cities",
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                success: function (clist) {
                    layer.msg("1");
                    var chtml = "<option value=''>请选择1</option>";
                    for (let i = 0; i < clist.length; i++) {
                        if ($("select[name='provinceid']").val() == clist[i].provinceid) {
                            chtml += "<option value=" + clist[i].cityid + ">" + clist[i].city + "</option>";
                        }
                    }
                    $("select[name='cityid']").html(chtml);

                    form.render('select'); //这个很重要,没有这个新的option不会显示
                    layer.msg(chtml);

                }, error: function () {
                    layer.msg("添加请求失败2");
                }
            });
        });

        //当select发生改变时
        form.on('select(cityid)', function (dat) {
            $.ajax({
                type: "post",
                url: "areas",
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                success: function (alist) {
                    var ahtml = "<option value=''>请选择1</option>";
                    for (let i = 0; i < alist.length; i++) {
                        if ($("select[name='cityid']").val() == alist[i].cityid) {
                            ahtml += "<option value=" + alist[i].areaid + ">" + alist[i].area + "</option>";
                        }
                    }
                    $("select[name='areaid']").html(ahtml);

                    layer.msg(ahtml);
                    form.render('select'); //这个很重要,没有这个新的option不会显示
                }, error: function () {
                    layer.msg("添加请求失败3");
                }
            });




        });

        //当select发生改变时
        form.on('select(areaid)', function () {
            // console.log({provincesId:$("#provinceid").val(),citiesId:$("#cityid").val(),areasId:$("#areaid").val(),stuId:$("#stuId").val()})
            //修改省、市、县
            $.ajax({
                type: 'post',
                url: 'updStu',
                contentType:'application/json;charset=utf-8',
                dataType:'json',
                data:JSON.stringify({provincesId:$("#provinceid").val(),citiesId:$("#cityid").val(),areasId:$("#areaid").val(),stuId:$("#stuId").val()}),
                success(result){
                    layer.alert(result.message, {icon: 1});
                    form.render();
                    // window.location.reload();

                },error(result){
                    layer.alert(result.message, {icon: 2});

                }
            });


        })
        //当select发生改变时
        form.on('select(idType)', function () {
            // console.log({provincesId:$("#provinceid").val(),citiesId:$("#cityid").val(),areasId:$("#areaid").val(),stuId:$("#stuId").val()})
            //修改省、市、县
            $.ajax({
                type: 'post',
                url: 'updStu',
                contentType:'application/json;charset=utf-8',
                dataType:'json',
                data:JSON.stringify({idType:$("#idTypes").val(),stuId:$("#stuId").val()}),
                success(result){
                    layer.alert(result.message, {icon: 1});
                    form.render();
                    // window.location.reload();

                },error(result){
                    layer.alert(result.message, {icon: 2});

                }
            });


        })



        //输入框获得焦点
        $("input").focus(function(){
            $("input").css("background-color","#FFFFCC");
        });

        //失去焦点
        $("input").blur(function(){
            $("input").css("background-color","#fff");


            $.ajax({
                type: 'post',
                url: 'updStu',
                contentType:'application/json;charset=utf-8',
                dataType:'json',
                data:JSON.stringify({stuName:$("#stuName").val(),
                                            stuId:$("#stuId").val(),
                                            detailAddr:$("#detailDddr").val(),
                                            IdNum:$("#IdNum").val,
                                            mobile:$("#mobile").val,
                                            email:$("#email").val,
                                            sex:$('input[name="sex"]:checked').val()
                                        }),
                success(result){
                    layer.alert(result.message, {icon: 1});
                    form.render();
                    // window.location.reload();

                },error(result){
                    layer.alert(result.message, {icon: 2});

                }
            });

        });

        $("#addstu").click(function(){
            let gradeGradeName = $("#gradeGradeName").val();
            console.log(gradeGradeName);
            var mainIndex =layer.open({
                type: 2,//弹出层类型
                title: "所在班级的学生信息",
                shade: 0,
                shadeClose:true,
                area: ['800px', '600px'],
                contentType: 'application/json',
                content: 'StuandGra/'+gradeGradeName, //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
                success: function () {}

            })
        })
        $("#gsAll").click(function() {
            let dormitoryDormitoryName = $("#dormitoryDormitoryName").val();
            console.log(dormitoryDormitoryName);
            var mainIndex =layer.open({
                type: 2,//弹出层类型
                title: "宿舍详情+本宿舍所有学生信息",
                shade: 0,
                shadeClose:true,
                area: ['800px', '600px'],
                contentType: 'application/json',
                content: 'StuandDor/'+dormitoryDormitoryName, //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
                success: function () {}

            })
        })
    })